import os
import shutil
import sys
import string
import zipfile

class WindowsPublisher:
    def __init__(self, version):
        shutil.copytree('../corelib', './kratos'+version+'/corelib')
        shutil.copytree('../engine', './kratos'+version+'/engine')
        shutil.copytree('../plugin', './kratos'+version+'/plugin')
        shutil.copytree('../template', './kratos'+version+'/template')
        shutil.copytree('../tools', './kratos'+version+'/tools')
        shutil.copytree('../vsproj', './kratos'+version+'/vsproj')
        shutil.copytree('../protocol_files', './kratos'+version+'/protocol_files')

        self.clenaup_corelib('./kratos'+version+'/corelib/')
        self.clenaup_engine('./kratos'+version+'/engine/')
        self.clenaup_plugin('./kratos'+version+'/plugin/')
        self.cleanup_vsproj('./kratos'+version+'/vsproj/')
        self.cleanup_protocol_files('./kratos'+version+'/protocol_files/')

    def remove_via_ext(self, dir, names):
        for file in os.listdir(dir):
            path = os.path.join(dir, file)
            delete = False
            if not os.path.isdir(path):
                for name in names:
                    if path.endswith(name):
                        delete = True
                        break
                if delete:
                    os.remove(path)
            else:
                if path != '.' and path != '..':
                    self.remove_via_ext(path + '/', names)

    def clenaup_engine(self, dir):
        self.remove_via_ext(dir, ('.c', '.cc', '.cpp', '.inl', '.md', '.txt', '.fbs', '.log', '.ilk', '.iobj', '.ipdb', '.pdb', 'flatc.exe', '_generated.h'))
        self.remove_via_ext(os.path.join(dir, 'bin', 'debug', 'module'), ('.dll', '.cfg'))
        self.remove_via_ext(os.path.join(dir, 'bin', 'release', 'module'), ('.dll', '.cfg'))
        if os.path.exists(os.path.join(dir, 'bin', 'debug', 'module', 'example1.service')):
            shutil.rmtree(os.path.join(dir, 'bin', 'debug', 'module', 'example1.service'))
        if os.path.exists(os.path.join(dir, 'bin', 'release', 'module', 'example2.service')):
            shutil.rmtree(os.path.join(dir, 'bin', 'release', 'module', 'example2.service'))

    def clenaup_corelib(self, dir):
        self.remove_via_ext(dir, ('.c', '.cc', '.cpp', '.inl', '.md', '.txt', '.fbs', '.log', '.ilk', '.iobj', '.ipdb', '.pdb', 'flatc.exe', '_generated.h'))

    def clenaup_plugin(self, dir):
        self.remove_via_ext(dir, ('.c', '.cc', '.cpp', '.inl', '.md', '.txt', '.fbs', '.log', '.ilk', '.iobj', '.ipdb', '.pdb', 'flatc.exe', '_generated.h'))

    def cleanup_protocol_files(self, dir):
        shutil.rmtree(os.path.join(dir, 'robot'))
        shutil.rmtree(os.path.join(dir, 'example'))
        shutil.rmtree(os.path.join(dir, 'example_service'))
        os.remove(os.path.join(dir, 'protobuf','example.proto'))
        os.remove(os.path.join(dir, 'message','example.message'))
        os.remove(os.path.join(dir, 'message','example_service.message'))
        self.remove_via_ext(os.path.join(dir, 'common'), ('.cc', '.h'))
        os.chdir(os.path.abspath(dir))
        os.system('python build_protocol.py')
        os.chdir(os.path.abspath('../../'))
        open(os.path.join(dir, 'dispatcher', 'dispatcher.site'), 'w').close()
        
    def cleanup_vsproj(self, dir):
        shutil.rmtree(os.path.join(dir, 'corelib'))
        shutil.rmtree(os.path.join(dir, 'engine'))
        shutil.rmtree(os.path.join(dir, 'example'))
        shutil.rmtree(os.path.join(dir, 'example_service'))
        shutil.rmtree(os.path.join(dir, 'plugin_template'))
        if os.path.exists(os.path.join(dir, 'plugin')):
            shutil.rmtree(os.path.join(dir, 'plugin'))
        shutil.rmtree(os.path.join(dir, 'solution'))
        if os.path.exists(os.path.join(dir, 'unittest')):
            shutil.rmtree(os.path.join(dir, 'unittest'))
        shutil.rmtree(os.path.join(dir, 'locator', 'Debug'))
        shutil.rmtree(os.path.join(dir, 'locator', 'Release'))
        shutil.rmtree(os.path.join(dir, 'gateway', 'Debug'))
        shutil.rmtree(os.path.join(dir, 'gateway', 'Release'))
        if os.path.exists(os.path.join(dir, 'locator', 'locator.vcxproj.user')):
            os.remove(os.path.join(dir, 'locator', 'locator.vcxproj.user'))
        if os.path.exists(os.path.join(dir, 'gateway', 'gateway.vcxproj.user')):
            os.remove(os.path.join(dir, 'gateway', 'gateway.vcxproj.user'))
        self.remove_via_ext(os.path.join(dir, 'gateway'), ('.obj', '.c', '.cc', '.cpp', '.inl', '.md', '.txt', '.fbs', '.log', '.ilk', '.iobj', '.ipdb', '.pdb', 'flatc.exe', '_generated.h'))
        self.remove_via_ext(os.path.join(dir, 'locator'), ('.obj', '.c', '.cc', '.cpp', '.inl', '.md', '.txt', '.fbs', '.log', '.ilk', '.iobj', '.ipdb', '.pdb', 'flatc.exe', '_generated.h'))
    
    def walkZipFile(self, dir, fileList):
        files = os.listdir(dir)
        for file in files:
            if os.path.isdir(dir+'/'+file):
                self.walkZipFile(dir+'/'+file,fileList)
            else:
                fileList.append(dir+'/'+file)
    
    def archive(self, dir, outputFile):
        f = zipfile.ZipFile(outputFile,'w',zipfile.ZIP_DEFLATED)
        fileLists = []
        self.walkZipFile(dir, fileLists)
        for file in fileLists:
            f.write(file)
        f.close()
        shutil.rmtree(dir)

class LinuxPublisher:
    pass

if __name__ == '__main__':
    if os.path.exists('kratos' + sys.argv[1]):
        shutil.rmtree('kratos' + sys.argv[1])
    if os.path.exists('kratos' + sys.argv[1] + '.zip'):
        os.remove('kratos' + sys.argv[1] + '.zip')
    publisher = WindowsPublisher(sys.argv[1])
    publisher.archive('kratos' + sys.argv[1], 'kratos' + sys.argv[1] + '.zip')
